package com.site.dao.impl;

import com.site.dao.OutStockDetailDao;
import com.site.pojo.OutStockDetail;
import com.site.pojo.ReceiveDetail;
import com.site.utils.JDBCUtils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * OutStockDetail DAO实现类
 */
public class OutStockDetailDaoImpl implements OutStockDetailDao {
    //查询所有
    @Override
    public List<OutStockDetail> selectAll() {
        String sql = "select * from out_stock_detail";
        List<OutStockDetail> list = new ArrayList<OutStockDetail>();
        ResultSet rs = null;
        Connection conn = null;
        try{
            conn = JDBCUtils.getConnection();
            rs = JDBCUtils.executeQuery(conn,sql);
            while (rs.next()){
                OutStockDetail outStockDetail = new OutStockDetail();
                outStockDetail.setId(rs.getLong("id"));
                outStockDetail.setOid(rs.getLong("oid"));
                outStockDetail.setEid(rs.getLong("eid"));
                outStockDetail.setGoodsId(rs.getLong("goods_id"));
                outStockDetail.setOutStockCount(rs.getLong("out_stock_count"));
                outStockDetail.setUnit(rs.getString("unit"));
                outStockDetail.setOprTime(rs.getTimestamp("opr_time"));
                list.add(outStockDetail);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.close(rs,null,conn);
        }
        return list;
    }
    //按id查询
    @Override
    public OutStockDetail selectByid(Long id) {
        String sql = "select * from out_stock_detail where id=?";
        OutStockDetail outStockDetail = new OutStockDetail();
        ResultSet rs = null;
        Connection conn = null;
        try{
            conn = JDBCUtils.getConnection();
            rs = JDBCUtils.executeQuery(conn,sql,id);
            while (rs.next()){
                outStockDetail.setId(rs.getLong("id"));
                outStockDetail.setOid(rs.getLong("oid"));
                outStockDetail.setEid(rs.getLong("eid"));
                outStockDetail.setGoodsId(rs.getLong("goods_id"));
                outStockDetail.setOutStockCount(rs.getLong("out_stock_count"));
                outStockDetail.setUnit(rs.getString("unit"));
                outStockDetail.setOprTime(rs.getTimestamp("opr_time"));
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.close(rs,null,conn);
        }
        return outStockDetail;
    }
    //查询一个出库单下所属的出库明细单
    @Override
    public List<OutStockDetail> selectByoid(Long oid) {
        String sql = "select * from out_stock_detail where oid=?";
        List<OutStockDetail> list = new ArrayList<OutStockDetail>();
        ResultSet rs = null;
        Connection conn = null;
        try{
            conn = JDBCUtils.getConnection();
            rs = JDBCUtils.executeQuery(conn,sql,oid);
            while (rs.next()){
                OutStockDetail outStockDetail = new OutStockDetail();
                outStockDetail.setId(rs.getLong("id"));
                outStockDetail.setOid(rs.getLong("oid"));
                outStockDetail.setEid(rs.getLong("eid"));
                outStockDetail.setGoodsId(rs.getLong("goods_id"));
                outStockDetail.setOutStockCount(rs.getLong("out_stock_count"));
                outStockDetail.setUnit(rs.getString("unit"));
                outStockDetail.setOprTime(rs.getTimestamp("opr_time"));
                list.add(outStockDetail);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.close(rs,null,conn);
        }
        return list;
    }
    //无事务，新增一个出库明细单
    @Override
    public int addOutStockDetail(OutStockDetail outStockDetail) {
        int num = 0;
        String sql = "insert into out_stock_detail(oid,eid,goods_id,rtid,out_stock_count,unit,opr_time) " +
                "values(?,?,?,?,?,?,?)";
        Object[] param = {outStockDetail.getOid(),outStockDetail.getEid(),outStockDetail.getGoodsId()
                ,outStockDetail.getRtid(),outStockDetail.getOutStockCount(),outStockDetail.getUnit(),outStockDetail.getOprTime()};
        try{
            num = JDBCUtils.executeUpdate(sql,param);
        }catch (Exception e){
            e.printStackTrace();
        }
        return num;
    }
    //有事务，新增一个出库明细单
    @Override
    public int addOutStockDetail(Connection conn, OutStockDetail outStockDetail) {
        int num = 0;
        String sql = "insert into out_stock_detail(oid,eid,goods_id,rtid,out_stock_count,unit,opr_time) " +
                "values(?,?,?,?,?,?)";
        Object[] param = {outStockDetail.getOid(),outStockDetail.getEid(),outStockDetail.getGoodsId()
                ,outStockDetail.getRtid(),outStockDetail.getOutStockCount(),outStockDetail.getUnit(),outStockDetail.getOprTime()};
        try{
            num = JDBCUtils.executeUpdate(conn,sql,param);
        }catch (Exception e){
            e.printStackTrace();
        }
        return num;
    }
    //无事务，通过id删除出库明细单
    @Override
    public int deleteOutStockDetailByid(Long id) {
        int num = 0;
        String sql = "delete from out_stock_detail where id=?";
        Object[] param = {id};
        try{
            num = JDBCUtils.executeUpdate(sql,param);
        }catch (Exception e){
            e.printStackTrace();
        }
        return num;
    }
    //有事务，通过id删除出库明细单
    @Override
    public int deleteOutStockDetailByid(Connection conn, Long id) {
        int num = 0;
        String sql = "delete from out_stock_detail where id=?";
        Object[] param = {id};
        try{
            num = JDBCUtils.executeUpdate(conn,sql,param);
        }catch (Exception e){
            e.printStackTrace();
        }
        return num;
    }
    //无事务，,通过outStockDetail.id修改出库明细单
    @Override
    public int updateOutStockDetail(OutStockDetail outStockDetail) {
        int num = 0;
        String sql = "update out_stock_detail set oid=?,eid=?,goods_id=?,rtid=?,out_stock_count=?,unit=?,opr_time=?" +
                " where id=?";
        Object[] param = {outStockDetail.getOid(),outStockDetail.getEid(),outStockDetail.getGoodsId()
                ,outStockDetail.getRtid(),outStockDetail.getOutStockCount(),outStockDetail.getUnit(),outStockDetail.getOprTime(),
                outStockDetail.getId()};
        try{
            num = JDBCUtils.executeUpdate(sql,param);
        }catch (Exception e){
            e.printStackTrace();
        }
        return num;
    }
    //有事务，,通过outStockDetail.id修改出库明细单
    @Override
    public int updateOutStockDetail(Connection conn, OutStockDetail outStockDetail) {
        int num = 0;
        String sql = "update out_stock_detail set oid=?,eid=?,goods_id=?,rtid=?,out_stock_count=?,unit=?,opr_time=?" +
                " where id=?";
        Object[] param = {outStockDetail.getOid(),outStockDetail.getEid(),outStockDetail.getGoodsId()
                ,outStockDetail.getRtid(),outStockDetail.getOutStockCount(),outStockDetail.getUnit(),outStockDetail.getOprTime(),
                outStockDetail.getId()};
        try{
            num = JDBCUtils.executeUpdate(conn,sql,param);
        }catch (Exception e){
            e.printStackTrace();
        }
        return num;
    }
}
